package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * dish业务层实现类
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    DishFlavorService dishFlavorService;

    @Autowired
    CategoryService categoryService;
    @Override
    public void saveDish(DishDto dishDto) {
        //将关于dish表中的数据保存到数据库中
        this.save(dishDto);
        //获取dishdto中口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //若菜品添加口味,获取dish表中的id,将dish中的id和口味存储到dishflavor中
        if(flavors != null && flavors.size() > 0){
            Long dtoId = dishDto.getId();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dtoId);
                dishFlavorService.save(flavor);
            }
        }

    }

    @Override
    public Page<DishDto> listDish(Integer page, Integer pageSize, String name) {
        //创建page<Dish>对象,将页数page,pageSize封装到page对象
        Page<Dish> dishPage = new Page<>(page, pageSize);
        //创建条件构造器,name like %?%
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //执行sql语句 select * from dish where name like %?% limit ?
        this.page(dishPage,queryWrapper);
        //创建page<DishDto>对象
        Page<DishDto> dishDtoPage = new Page<>();
        //使用BeanUtils,将dishPage中除了records的属性复制到dishDtoPage
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        //获取dishPage中records属性
        List<Dish> dishes = dishPage.getRecords();
        //dishes开启stream流
        List<DishDto> dishDtos = dishes.stream().map(dish -> {
            //获取dish中category_id
            Long id = dish.getCategoryId();
            //根据id获取category
            Category category = categoryService.getById(id);
            //新建dishdto对象,将dish中的属性复制给dishDto
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //获取category中的name
            dishDto.setCategoryName(category.getName());
            return dishDto;
            //将dishdto封装回listjihe
        }).collect(Collectors.toList());
        //将disdtos赋给dishdtopage中的records中
        dishDtoPage.setRecords(dishDtos);
        return dishDtoPage;
    }

    @Override
    public DishDto getDishById(Long id) {
        //通过id查询dish表获取dish数据封装对象
        Dish dish = this.getById(id);
        //创建dishdto对象
        DishDto dishDto = new DishDto();
        //将dish中的属性复制给dishdto
        BeanUtils.copyProperties(dish,dishDto);
        //获取dish中category_id
        Long categoryId = dish.getCategoryId();
        //根据category_id查询category中的id,封装category
        Category category = categoryService.getById(categoryId);
        //将category中的name添加到dishdto中category属性
        dishDto.setCategoryName(category.getName());
        //创建条件构造器,根据dish_id获取口味表的数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
        //将口味数据添加到地上dto中
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    @Override
    public void updateDish(DishDto dishDto) {
        //通过id更新dish表中的数据
        this.updateById(dishDto);
        //获取dishdto中的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //创建条件构造器,根据dish_id获取口味表的数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
        //开启stream流获取口味的id
        List<Long> ids = dishFlavors.stream().map(DishFlavor::getId).collect(Collectors.toList());
        //删除口味表中id在ids中的数据
        dishFlavorService.removeByIds(ids);
        //将新数据更新到口味表中
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void deleteDish(List<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public void changeStatus(Integer status, List<Long> ids) {
        // update dish set status = ? where id in (1,2,3)
        LambdaUpdateWrapper<Dish> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.set(Dish::getStatus,status);// set status = ?
        queryWrapper.in(Dish::getId,ids);//  where id in (1,2,3)
        this.update(queryWrapper);
    }
}
